package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.aid.AidDonation;
import com.ruoyi.system.service.aid.IAidDonationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 物资捐赠Controller
 */
@RestController
@RequestMapping("/aid/donation")
public class AidDonationController extends BaseController {
    @Autowired
    private IAidDonationService aidDonationService;
    /**
     * 获取可用的捐赠列表
     */
    @PreAuthorize("@ss.hasPermi('aid:donation:query')")
    @GetMapping("/available")
    public AjaxResult getAvailableDonations() {
        AidDonation donation = new AidDonation();
        donation.setStatus("0"); // 状态为正常的
        List<AidDonation> list = aidDonationService.selectAidDonationList(donation);
        return success(list);
    }
    /**
     * 查询物资捐赠列表
     */
    @PreAuthorize("@ss.hasPermi('aid:donation:list')")
    @GetMapping("/list")
    public TableDataInfo list(AidDonation aidDonation) {
        startPage();
        List<AidDonation> list = aidDonationService.selectAidDonationList(aidDonation);
        return getDataTable(list);
    }

    /**
     * 导出物资捐赠列表
     */
    @PreAuthorize("@ss.hasPermi('aid:donation:export')")
    @Log(title = "物资捐赠", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AidDonation aidDonation) {
        List<AidDonation> list = aidDonationService.selectAidDonationList(aidDonation);
        ExcelUtil<AidDonation> util = new ExcelUtil<AidDonation>(AidDonation.class);
        return util.exportExcel(list, "donation");
    }

    /**
     * 获取物资捐赠详细信息
     */
    @PreAuthorize("@ss.hasPermi('aid:donation:query')")
    @GetMapping(value = "/{donationId}")
    public AjaxResult getInfo(@PathVariable("donationId") Long donationId) {
        return AjaxResult.success(aidDonationService.selectAidDonationById(donationId));
    }

    /**
     * 新增物资捐赠
     */
    @PreAuthorize("@ss.hasPermi('aid:donation:add')")
    @Log(title = "物资捐赠", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AidDonation aidDonation) {
        return toAjax(aidDonationService.insertAidDonation(aidDonation));
    }

    /**
     * 修改物资捐赠
     */
    @PreAuthorize("@ss.hasPermi('aid:donation:edit')")
    @Log(title = "物资捐赠", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AidDonation aidDonation) {
        return toAjax(aidDonationService.updateAidDonation(aidDonation));
    }

    /**
     * 删除物资捐赠
     */
    @PreAuthorize("@ss.hasPermi('aid:donation:remove')")
    @Log(title = "物资捐赠", businessType = BusinessType.DELETE)
    @DeleteMapping("/{donationIds}")
    public AjaxResult remove(@PathVariable Long[] donationIds) {
        return toAjax(aidDonationService.deleteAidDonationByIds(donationIds));
    }
} 